<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category_m extends CI_Model {
	const TBL_CAT = 'category';
	const TBL_USER_CAT = 'user_category';

	/**
     * 构造函数
     *
     * @access public
     * @return void
     */
    public function __construct()
    {
       parent::__construct();
    }

    public function get_all_category(){
    	$ret = array();
    	$other_id = 0;
    	$other = array();

    	$this->db->order_by('id asc');
    	$query = $this->db->get(self::TBL_CAT);
    	foreach ($query->result_array() as $row){
    		if($row['cat_level'] == 1){
    			if(!isset($ret[$row['id']])){
    				$ret[$row['id']] = array();
    			}
    			if(!isset($ret[$row['id']]['data'])){
    				$ret[$row['id']]['data'] = array();
    			}
    			$ret[$row['id']]['cat_id'] = $row['id'];
    			$ret[$row['id']]['cat_name'] = $row['cat_name'];
    			if('其他' == $row['cat_name']){
    				$other_id = $row['id'];
    			}
    		}elseif ($row['cat_level'] == 2){
    			if(!isset($ret[$row['cat_parent']])){
    				$ret[$row['cat_parent']] = array();
    			}
    			if(!isset($ret[$row['cat_parent']]['data'])){
    				$ret[$row['cat_parent']]['data'] = array();
    			}
    			$item = array();
    			$item['id'] = $row['id'];
    			$item['cat_name'] = $row['cat_name'];
    			if('其他' == $row['cat_name']){
    				$other[$row['cat_parent']] = $item;
    			}else{
    				$ret[$row['cat_parent']]['data'][] = $item;
    			}
    		}
    	}
    	//将"其他"挪到最后一项
    	$elem_other = $ret[$other_id];
    	unset($ret[$other_id]);
    	$ret[$other_id] = $elem_other;
    	
    	foreach ($ret as $parent_id => $lev1_cat){
    		if(isset($other[$parent_id])){
    			$ret[$parent_id]['data'][$other[$parent_id]['id']] = $other[$parent_id];
    		}
    	}

    	return $ret;
    }
	
    function get_category($all_id = array()){
    	if(!is_array($all_id) || !$all_id){
    		return array();
    	}
    	
    	$this->db->where_in('id',$all_id);
		$query = $this->db->get(self::TBL_CAT);
		
		$ret = array();
		foreach ($query->result_array() as $row){
			$ret[$row['id']] = $row;
		}
    	return  $ret;
    }

	function get_used_category(){
		$this->db->distinct('cat_level2');
		$this->db->where('cat_level2 >', 0);
		$query = $this->db->get('goods');
		$ret = array();
		foreach ($query->result_array() as $row){
			$ret[] = intval($row['cat_level2']);
		}
		return $ret;
	}

	function update_category($cat_data){
		if (intval($cat_data->cat_parent) === 0){
			return FALSE;
		}
		$used_category = $this->get_used_category();
		$in_used = in_array(intval($cat_data->id), $used_category);
		if ($in_used){
			// can only update category name
			$this->db->where('id', $cat_data->id);
			$allow_data = array('cat_name' => $cat_data->cat_name);
			$this->db->update(self::TBL_CAT, $allow_data);
		}else{
			$this->db->where('id', $cat_data->id);
			$this->db->update(self::TBL_CAT, $cat_data);
		}
	}

	function delete_category($id){
		$used_category = $this->get_used_category();
		if (! in_array(intval($id), $used_category)){
			$this->db->where('id', $id);
			$this->db->where('cat_level', 2); //ONLY delete level 2 category
			$this->db->delete(self::TBL_CAT);
			return TRUE;
		}else{
			return FALSE;
		}
	}
    
    function add_category($name, $parent, $level = 2){
    	$this->db->set('cat_name', $name);
    	$this->db->set('cat_level', $level);
    	$this->db->set('cat_parent', $parent);
    	if($this->db->insert(self::TBL_CAT)){
			return $this->db->insert_id();
		}
		return false;
    }

    function get_user_valid_category($user_id){
    	$ret = array();

    	$this->db->where('user_id', $user_id);
    	$query = $this->db->get(self::TBL_USER_CAT);
    	foreach ($query->result_array() as $row){
    		$ret[] = $row['cat_id'];
    	}

    	return $ret;
    }
    
    function get_users_valid_category($user_ids = array()){
    	$ret = array();

    	$this->db->where_in('user_id', $user_ids);
    	$query = $this->db->get(self::TBL_USER_CAT);
    	foreach ($query->result_array() as $row){
    		if(!isset($ret[$row['user_id']])){
    			$ret[$row['user_id']] = array();
    		}
    		
    		$ret[$row['user_id']][] = $row['cat_id'];
    	}
		$query->free_result();
		
    	return $ret;
    }

	function get_level_one_category(){
		$table = self::TBL_CAT;
		$query = $this->db->query("SELECT id, cat_name FROM $table WHERE cat_level = 1;");
		$map = array();
		foreach ($query->result() as $row){
			$map[$row->cat_name] = $row->id;
		}
		return $map;
	}

	function get_level_two_category($offset = 0, $row_count = 0, $name = ''){
		$this->db->select('c2.id, c2.cat_name');
		$this->db->select('c1.cat_name AS cat_parent');
		$this->db->from('category AS c2');
		$this->db->join('category AS c1', 'c1.id = c2.cat_parent', 'inner');
		if (strlen($name) > 0)
		{
			$this->db->like('c2.cat_name', $name, 'both');
		}
		$this->db->where('c2.cat_level = 2');
		$this->db->order_by('c2.id desc');
		return $this->db->get('', $row_count, $offset);
	}

	function get_total_level_two_category($name){
		$this->db->select("COUNT(*) AS total", FALSE);
		if (strlen($name) > 0)
		{
			$this->db->like('cat_name', $name, 'both');
		}
		$this->db->where('cat_level = 2');
		$query = $this->db->get(self::TBL_CAT);
		$res = $query->result_array();
		return intval($res[0]['total']);
	}
	
	function get_child_category($parent_cat){
		$ret = array();
		$other_id=0;
		
		$this->db->where('cat_parent', $parent_cat);
		$query = $this->db->get(self::TBL_CAT);
		foreach ($query->result_array() as $row){
			if($row['cat_name'] == '其他'){
				$other_id = $row['id'];
			}
			$ret[$row['id']] = $row['cat_name'];
		}
		if($other_id > 0){
			$other_item = $ret[$other_id];
			unset($ret[$other_id]);
			$ret[$other_id] = $other_item;
		}

		return $ret;
	}
	
	function get_cat_by_name_and_parent($name, $parent){
		$this->db->where('cat_name', $name);
		$this->db->where('cat_parent', $parent);
		$query = $this->db->get(self::TBL_CAT);
		$row = $query->row_array();
		return $query->num_rows() ? $row : false;
	}
	
	function add_lev2cat($name, $parent){
		$cat = $this->get_cat_by_name_and_parent($name, $parent);
		if($cat){
			return false;
		}
		
		$insert_id = $this->add_category($name,$parent);
		
		return $insert_id;
	}

    function get_user_all_category_status($user_id=0){
    	$user_id = intval($user_id);
    	if($user_id > 0){
    		$valid_cat = $this->get_user_valid_category($user_id);
    	}
    	
    	$all_cat_info = $this->get_all_category();
    	foreach ($all_cat_info as $key => $val){
    		foreach ($val['data'] as $item_key => $item){
    			$all_cat_info[$key]['data'][$item_key]['valid'] =
    				($user_id <= 0 || in_array($item['id'],$valid_cat)) ? 1 : 0;
    		}
    	}
    	return $all_cat_info;
    }

    function set_user_category($user_id, $new_category = array()){
    	$valid_cat = $this->get_user_valid_category($user_id);

    	$insert_cat = array();
    	$del_cat = array();
    	foreach ($new_category as $new_id){
    		if(!in_array($new_id, $valid_cat)){
    			$insert_cat[] = $new_id;
    		}
    	}
    	foreach ($valid_cat as $old_id){
    		if(!in_array($old_id, $new_category)){
    			$del_cat[] = $old_id;
    		}
    	}

    	foreach ($insert_cat as $cat_id){
    		$data = array(
               'user_id' => $user_id ,
               'cat_id' => $cat_id
            );
			$this->db->insert(self::TBL_USER_CAT, $data);
    	}

    	$this->db->where('user_id', $user_id);
    	$this->db->where_in('cat_id', $del_cat);
    	$this->db->delete(self::TBL_USER_CAT);
    }
}
